﻿using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;

namespace DataServices
{
	[DataServiceKey("Name")]
	public class Inductee
	{
		public string Name { get; set; }
		public bool Group { get; set; }
		public int YearInducted { get; set; }
		public List<Song> Songs { get; set; }

		public static List<Inductee> MakeInducteeList()
		{
			return (new List<Inductee>()
		{
			new Inductee()
			{
				Name = "Rolling Stones",
				Group = false,
				YearInducted = 1990,
				Songs = new List<Song>()
			},
			new Inductee()
			{
				Name = "Beatles",
				Group = false,
				YearInducted = 1986,
				Songs = new List<Song>()
			}
		});
		}
	}

	[DataServiceKey("SongTitle")]
	public class Song
	{
		public string SongTitle { get; set; }

		public static List<Song> MakeSongList()
		{
			return (new List<Song>()
		{
			new Song(){SongTitle="Satisfaction"},
			new Song(){SongTitle="All you need is love"},
		});
		}
	}

	public class AssignInducteesToSongs
	{
		public static void Assign(List<Inductee> inductee, List<Song> songs)
		{
			inductee[0].Songs.Add(songs[0]);
			inductee[1].Songs.Add(songs[1]);
		}
	}
	/// <summary>
	/// Summary description for MyDataModel
	/// </summary>
	public class MyDataModel
	{
		static List<Inductee> inductees;
		static List<Song> songs;

		static MyDataModel()
		{
			inductees = Inductee.MakeInducteeList();
			songs = Song.MakeSongList();
			AssignInducteesToSongs.Assign(inductees, songs);
		}

		public IQueryable<Inductee> Inductees
		{
			get
			{
				return inductees.AsQueryable();
			}
		}

		public IQueryable<Song> Songs
		{
			get
			{
				return songs.AsQueryable();
			}
		}
	}

	[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
	public class DemoOb : DataService<MyDataModel>
	{
		public static void InitializeService(DataServiceConfiguration config)
		{
			config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
			config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
			config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
		}
	}
}
